package com.luyuan.gallery.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.luyuan.common.utils.DateUtils;
import com.luyuan.common.utils.bean.BeanUtils;
import com.luyuan.gallery.domain.ExhiComment;
import com.luyuan.gallery.mapper.ExhiCommentMapper;
import com.luyuan.gallery.param.CommentParam;
import com.luyuan.gallery.service.IExhiCommentService;
import com.luyuan.gallery.vo.HierarchicalCommentVO;

/**
 * 评论Service业务层处理
 * 
 * @author luyuan
 * @date 2025-11-10
 */
@Service
public class ExhiCommentServiceImpl implements IExhiCommentService {
    @Autowired
    private ExhiCommentMapper exhiCommentMapper;

    /**
     * 查询评论
     * 
     * @param id 评论主键
     * @return 评论
     */
    @Override
    public ExhiComment selectExhiCommentById(Long id) {
        return exhiCommentMapper.selectExhiCommentById(id);
    }

    /**
     * 查询评论列表
     * 
     * @param exhiComment 评论
     * @return 评论
     */
    @Override
    public List<ExhiComment> selectExhiCommentList(ExhiComment exhiComment) {
        return exhiCommentMapper.selectExhiCommentList(exhiComment);
    }

    /**
     * 新增评论
     * 
     * @param exhiComment 评论
     * @return 结果
     */
    @Override
    public int insertExhiComment(ExhiComment exhiComment) {
        exhiComment.setCreateTime(DateUtils.getNowDate());
        return exhiCommentMapper.insertExhiComment(exhiComment);
    }

    /**
     * 修改评论
     * 
     * @param exhiComment 评论
     * @return 结果
     */
    @Override
    public int updateExhiComment(ExhiComment exhiComment) {
        exhiComment.setUpdateTime(DateUtils.getNowDate());
        return exhiCommentMapper.updateExhiComment(exhiComment);
    }

    /**
     * 批量删除评论
     * 
     * @param ids 需要删除的评论主键
     * @return 结果
     */
    @Override
    public int deleteExhiCommentByIds(Long[] ids) {
        int count = 0;
        for (Long id : ids) {
            ExhiComment exhiComment = new ExhiComment();
            exhiComment.setId(id);
            exhiComment.setStatus(3); // 设置为已删除状态
            exhiComment.setUpdateTime(DateUtils.getNowDate());
            count += exhiCommentMapper.updateExhiCommentStatus(exhiComment);
        }
        return count;
    }

    /**
     * 删除评论信息
     * 
     * @param id 评论主键
     * @return 结果
     */
    @Override
    public int deleteExhiCommentById(Long id) {
        ExhiComment exhiComment = new ExhiComment();
        exhiComment.setId(id);
        exhiComment.setStatus(3); // 设置为已删除状态
        exhiComment.setUpdateTime(DateUtils.getNowDate());
        // TODO: 这里可以考虑级联删除子评论，暂未实现
        return exhiCommentMapper.updateExhiCommentStatus(exhiComment);
    }

    /**
     * 根据用户ID查询评论列表
     * 
     * @param userId 用户ID
     * @return 评论集合
     */
    @Override
    public List<ExhiComment> selectExhiCommentListByUserId(Long userId) {
        return exhiCommentMapper.selectExhiCommentListByUserId(userId);
    }

    /**
     * 根据目标ID查询评论列表
     * 
     * @param exhiComment 评论查询条件
     * @return 层级评论集合
     */
    @Override
    public List<HierarchicalCommentVO> selectExhiCommentListByTargetId(CommentParam commentParam) {
        ExhiComment exhiComment = new ExhiComment();
        BeanUtils.copyBeanProp(exhiComment, commentParam);
        List<HierarchicalCommentVO> allComments = exhiCommentMapper.selectExhiCommentListByTargetId(exhiComment);
        return allComments;
    }
}